package com.unicom.projt.query;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.unicom.projt.Home;
import com.unicom.projt.InputPhoneNumberPage;
import com.unicom.projt.Main;
import com.unicom.projt.R;
import com.unicom.projt.util.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-6
 * Time: 下午9:24
 */
public class QueryPackageRemainPage extends Activity implements View.OnClickListener {
    private TextView mPhoneNumber;
    private TextView mCustomerName;
    private TextView mPackageName;

    private SimpleAdapter listAdapter;
    private ListView mListView;
    private List<HashMap<String, String>> listDate = new ArrayList<HashMap<String, String>>();
    private int[] colors = new int[]{Color.WHITE, Color.LTGRAY};

    private Handler mHandler;

    private ProgressDialog mDialog;
    private static final int DIALOG_PROGRESS = 10;
    private QueryController mController;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pacage_remain);
        mHandler = new Handler();
        mController = new QueryController(this);
        initData();
        queryPackageRemain();
        this.findViewById(R.id.package_remain_back_btn).setOnClickListener(this);
        this.findViewById(R.id.package_remain_logout_btn).setOnClickListener(this);
    }

    private void initData() {
        mPhoneNumber = (TextView) this.findViewById(R.id.package_remain_phone_number);
        mCustomerName = (TextView) this.findViewById(R.id.package_remain_customer);
        mPackageName = (TextView) this.findViewById(R.id.package_remain_package);

        mListView = (ListView) this.findViewById(R.id.package_remain_list_view);


        listAdapter = new SimpleAdapter(this, listDate, R.layout.package_list_item,
                new String[]{"itemTitle", "itemValue", "itemUsed", "itemRemain"},
                new int[]{R.id.package_item_title, R.id.rectangleProgressBar, R.id.package_item_used, R.id.package_item_remain}) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                int colorPos = position % colors.length;
                view.setBackgroundColor(colors[colorPos]);
                return view;
            }
        };
        listAdapter.setViewBinder(new SimpleAdapter.ViewBinder() {
            @Override
            public boolean setViewValue(View view, Object o, String s) {
                if (view instanceof ProgressBar) {
                    Double valueUsed = Double.valueOf(s.split(",")[0]);
                    Double valueTotal = Double.valueOf(s.split(",")[1]);
                    Double percentage = valueUsed / valueTotal * 100;
                    int process = percentage.intValue();
                    ((ProgressBar) view).setProgress(process);
                    ((ProgressBar) view).setMax(100);
                    return true;
                }
                return false;
            }
        });
        mListView.setAdapter(listAdapter);

    }

    private void queryPackageRemain() {
//        initTestData();
        showDialog(DIALOG_PROGRESS);
        mController.queryPR(UserHolder.phoneNumber, DateUtils.formatOrderDateTime(new Date()));
    }

    public void updatePackageRemainInfo(String packageName, List<String> records) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mPhoneNumber.setText(UserHolder.phoneNumber);
                mCustomerName.setText(UserHolder.userName);
//                mPackageName.setText("WCDMA-66元A");
            }
        });
        listDate.clear();

        for(String record : records){
            HashMap<String, String> map = new HashMap<String, String>();
            String type=SimpleXmlParser.getElementsValue(record,"FlowType").get(0);
            String freeTotal=SimpleXmlParser.getElementsValue(record,"FreeTotle").get(0);
            String freeUsed=SimpleXmlParser.getElementsValue(record,"FreeUsed").get(0);
            String freeLeavings=SimpleXmlParser.getElementsValue(record,"FreeLeavings").get(0);
            String unit="";
            if("01".equals(type)){
                map.put("itemTitle", "3G上网流量");
                unit="MB";
            }else if("02".equals(type)){
                map.put("itemTitle", "3G语音电话");
                unit="分钟";
            }else if("03".equals(type)){
                map.put("itemTitle", "3G国内可视电话");
                unit="分钟";
            }else if("04".equals(type)){
                map.put("itemTitle", "3G短信");
                unit="条";
            }else if("05".equals(type)){
                map.put("itemTitle", "3G M值");
                unit="个";
            }else if("06".equals(type)){
                map.put("itemTitle", "3G T值");
                unit="个";
            }else if("08".equals(type)){
                map.put("itemTitle", "3G彩信");
                unit="条";
            }
            map.put("itemValue", freeUsed+","+freeTotal);
            map.put("itemUsed", "已用"+freeUsed+unit);
            map.put("itemRemain", "剩余"+freeLeavings+unit);
            listDate.add(map);
        }
        

        listAdapter.notifyDataSetChanged();
    }

    private void initTestData(){
        HashMap<String, String> map = new HashMap<String, String>();
                map.put("itemTitle", "语音通话时长");
        map.put("itemValue", "39,100");
        map.put("itemUsed", "已用39分钟");
        map.put("itemRemain", "剩余61分钟");
        listDate.add(map);

        map = new HashMap<String, String>();
        map.put("itemTitle", "3G上网流量");
        map.put("itemValue", "576.00,766.48");
        map.put("itemUsed", "已用576.00MB");
        map.put("itemRemain", "剩余190.46MB");
        listDate.add(map);

        map = new HashMap<String, String>();
        map.put("itemTitle", "视频通话时长");
        map.put("itemValue", "0,10");
        map.put("itemUsed", "已用0分钟");
        map.put("itemRemain", "剩余10分钟");
        listDate.add(map);
        map = new HashMap<String, String>();
        map.put("itemTitle", "视频通话时长");
        map.put("itemValue", "0,10");
        map.put("itemUsed", "已用0分钟");
        map.put("itemRemain", "剩余10分钟");
        listDate.add(map);
        map = new HashMap<String, String>();
        map.put("itemTitle", "视频通话时长");
        map.put("itemValue", "0,10");
        map.put("itemUsed", "已用0分钟");
        map.put("itemRemain", "剩余10分钟");
        listDate.add(map);
        map = new HashMap<String, String>();
        map.put("itemTitle", "视频通话时长");
        map.put("itemValue", "0,10");
        map.put("itemUsed", "已用0分钟");
        map.put("itemRemain", "剩余10分钟");
        listDate.add(map);

    }

    @Override
    public void onBackPressed() {
        PageJumpUtils.jump(this, Home.class);
        this.finish();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_PROGRESS:
                mDialog = new ProgressDialog(this);
                mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mDialog.setMessage("正在处理，请等待......");
                mDialog.setCancelable(false);
                mDialog.setOnKeyListener(new DialogInterface.OnKeyListener(){
                    public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
                            return true;
                        }
                        return false;
                    }
                });
                return mDialog;
            default:
                return null;
        }
    }

    public void hideProgressDialog() {
        if (mDialog != null) {
            removeDialog(DIALOG_PROGRESS);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.package_remain_back_btn:
                PageJumpUtils.jump(this, Home.class);
                this.finish();
                break;
            case R.id.package_remain_logout_btn:
                AlertDialogUtils.showYesNoDiaLog(this, "是否注销？", new AlertDialogUtils.Executor() {
                    @Override
                    public void execute() {
                        UserHolder.clean();
                        doLogout();
                    }
                });
                break;
        }
    }
    private void doLogout(){
        Intent intent = new Intent(this, InputPhoneNumberPage.class);
        this.startActivity(intent);
        this.finish();
    }
}
